<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>在线注册</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="width: 500px;margin: 0 auto">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="考生姓名" prop="applyName">
          <el-input v-model="ruleForm.applyName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="ruleForm.idNumber"></el-input>
        </el-form-item>
        <el-form-item label="考试项目" prop="title">
          <template>
            <el-select v-model="ruleForm.id" placeholder="请选择">
              <el-option
                  v-for="item in options"
                  :label="item.title"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style>
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        applyName:'',
        idNumber:'110101202304281239',
        id:''
      },
      options: '',
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        applyName: [
          { required: true, message: '请输入考生姓名', trigger: 'blur' },
          {  min: 2, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur'  }
        ],
        idNumber: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          {   pattern:/^[1-9]\d{5}(19\d{2}|20[0-2]\d)(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/ , message: '请输入正确的身份证号', trigger: 'blur'  }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /*向后端提交异步请求*/
          let  url="http://120.77.154.166:8080/examUser/front/add-new"
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '报名成功！',
                type: 'success'
              });
              this.resetForm(formName);
            }else if (jsonResult.state == 40900) {
              this.$alert(jsonResult.message, '报名失败,该考生已经报名', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '报名失败', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadExam() {
      let url = "http://120.77.154.166:8080/exam/list";
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          console.log(jsonResult.data)
          this.options = jsonResult.data;
        }
      });
    }
  },
  mounted() {
    this.loadExam();
  }
}
</script>
